<template>
  <div class="device-temp">
    <div>
      <label>{{ $t("route.DeviceManager") }}</label>
      <div></div>
      <button>
        <img /><label>{{ $t("route.Equipment_temperature") }}</label>
      </button>
    </div>

    <div>
      <div>
        <el-date-picker
          size="default"
          @change="getTempList"
          v-model="query.createTime"
          type="date"
          :placeholder="$t('route.Select_date')"
        />
        <el-radio-group
          v-model="query.timeRange"
          @change="getTempList"
          class="mgr-left-10"
        >
          <el-radio-button label="所有时间" />
          <el-radio-button label="2小时(单)" />
          <el-radio-button label="2小时(双)" />
        </el-radio-group>
      </div>

      <div>{{ device.kind }} {{ device.noseCode }}</div>
      <div>
        <el-button type="primary" @click="extClick">{{ $t("route.export") }}</el-button>
      </div>
    </div>
    <div style="margin-top: 20px">
      <div v-if="chartShow" id="myChart" style="height: 250px; width: 100%"></div>
    </div>
    <div>
      <div class="pagination">
        <el-pagination
          style="float: right"
          background
          @current-change="handlePageChange"
          :current-page="query.pageNum"
          :page-size="query.pageSize"
          layout="total, prev, pager, next"
          :total="query.total"
        >
        </el-pagination>
      </div>
      <el-table :data="tableData" :row-style="{ height: '60px' }">
        <el-table-column prop="createTime" :label="$t('route.time')" />
        <el-table-column prop="stdTemp" :label="$t('route.Standard_temperature')" />
        <el-table-column :label="$t('route.Left')">
          <template prop="temp1" #default="scope">
            <div :style="formatTempColorStyle(scope.row.stdTemp, scope.row.temp1)">
              {{ scope.row.temp1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="temp2" :label="$t('route.Medium')">
          <template prop="temp2" #default="scope">
            <div :style="formatTempColorStyle(scope.row.stdTemp, scope.row.temp2)">
              {{ scope.row.temp2 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="temp3" :label="$t('route.Right')">
          <template prop="temp3" #default="scope">
            <div :style="formatTempColorStyle(scope.row.stdTemp, scope.row.temp3)">
              {{ scope.row.temp3 }}
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import ParamUtils from "../../js/common/param-utils.js";
import http from "../../js/http/http.js";
import api from "../../js/http/api.js";
import CommonUtils from "../../js/common/common-utils.js";
import * as echarts from "echarts";

export default {
  data() {
    return {
      query: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
        createTime: new Date(),
        timeRange: "2小时(单)",
      },
      option: {},
      dataType: this.$t("route.energy_consumption"),
      xdata: [],
      ydata: [],
      device: {},
      tableData: [],
      chartShow: false,
    };
  },
  methods: {
    extClick() {
      var timeRange = null;
      if (this.query.timeRange == "2小时(单)") {
        timeRange = [
          "01:00",
          "03:00",
          "05:00",
          "07:00",
          "09:00",
          "11:00",
          "13:00",
          "15:00",
          "17:00",
          "19:00",
          "21:00",
          "23:00",
        ];
      } else if (this.query.timeRange == "2小时(双)") {
        timeRange = [
          "00:00",
          "02:00",
          "04:00",
          "06:00",
          "08:00",
          "10:00",
          "12:00",
          "14:00",
          "16:00",
          "18:00",
          "20:00",
          "22:00",
        ];
      }

      this.download(
        api.BASE_URI +
          api.EXT_TEMP_PRO +
          "?deviceId=" +
          this.device.id +
          "&date=" +
          this.query.createTime.format("yyyy-MM-dd") +
          "&timeRange=" +
          JSON.stringify(timeRange),
        this.$t("route.Equipment_temperature") + new Date().format("yyyy-MM-dd") + ".xlsx"
      );
    },
    download(url, fileName) {
      let x = new XMLHttpRequest();
      x.open("GET", encodeURI(url), true);
      x.responseType = "blob";
      x.onload = function (e) {
        let url = window.URL.createObjectURL(x.response);
        let a = document.createElement("a");
        a.href = url;
        a.download = fileName;
        a.click();
      };
      x.send();
    },
    handlePageChange(pageNum) {
      this.query.pageNum = pageNum;
      this.getTempList();
    },
    getTempList() {
      var timeRange = null;
      if (this.query.timeRange == "2小时(单)") {
        timeRange = [
          "01:00",
          "03:00",
          "05:00",
          "07:00",
          "09:00",
          "11:00",
          "13:00",
          "15:00",
          "17:00",
          "19:00",
          "21:00",
          "23:00",
        ];
      } else if (this.query.timeRange == "2小时(双)") {
        timeRange = [
          "00:00",
          "02:00",
          "04:00",
          "06:00",
          "08:00",
          "10:00",
          "12:00",
          "14:00",
          "16:00",
          "18:00",
          "20:00",
          "22:00",
        ];
      }

      http.post(
        api.GET_TEMP_LIST,
        {
          date: this.query.createTime.format("yyyy-MM-dd"),
          timeRange: JSON.stringify(timeRange),
          deviceId: this.device.id,
          pageNum: this.query.pageNum,
          pageSize: this.query.pageSize,
        },
        (data) => {
          this.tableData = data.data.tempList;
          this.query.total = data.data.total;
          var xData = [];
          var yData1 = [];
          var yData2 = [];
          var yData3 = [];
          for (var item of data.data.tempList) {
            xData.push(item.createTime);
            yData1.push(item.temp1);
            yData2.push(item.temp2);
            yData3.push(item.temp3);
          }
          var myChart = echarts.init(document.getElementById("myChart"));
          var series = [
            {
              data: yData1,
              type: "bar",
              name: "左",
              smooth: true,

              label: {
                show: true,
              },
            },
            {
              data: yData2,
              type: "bar",
              name: "中",
              smooth: true,
              label: {
                show: true,
              },
            },
            {
              data: yData3,
              type: "bar",
              name: "右",
              smooth: true,
              label: {
                show: true,
              },
            },
          ];
          var option = CommonUtils.initBarChartMultOption(
            this.$t("route.temperature"),
            xData,
            series
          );
          option.series[0].realtimeSort = false;
          myChart.setOption(option);
        }
      );
    },
    formatTempColorStyle(stdTemp, temp) {
      if (Math.abs(stdTemp - temp) > 6) return "color: #FF0000";
    },
  },
  created() {
    this.chartShow = true;
    this.device = JSON.parse(CommonUtils.getParam(this, "device", "setDevice"));
    this.getTempList();
  },
  destroyed() {
    this.chartShow = false;
  },
};
</script>

<style lang="scss" scoped>
.device-temp {
  color: #444444;
  font-size: 18px;
  padding-right: 20px;
  height: calc(100vh - 100px);
  // overflow: scroll;
  scrollbar-width: none;
  /* firefox */
  -ms-overflow-style: none;
  /* IE 10+ */
  overflow-x: hidden;
  overflow-y: auto;

  > div {
    display: flex;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;
  }

  > div:nth-of-type(1) {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;

    > label:nth-of-type(1) {
      font-weight: 600;
    }

    > div:nth-of-type(1) {
      width: 1px;
      background-color: #cccccc;
      height: 15px;
      margin: 0px 20px;
    }

    > button:nth-of-type(1) {
      background-image: url(../../assets/img/customer-1.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
      padding: 5px 10px;
      border: 0px;
      display: flex;
      color: #ffffff;
      flex-direction: row;
      align-items: center;
      font-weight: 800;

      > img {
        margin-right: 7px;
        width: 15px;
        height: 15px;
        content: url(../../assets/img/customer-2.png);
      }
    }
  }

  > div:nth-of-type(2) {
    display: flex;
    flex-direction: row;

    > div {
      flex: 1;
      text-align: center;
    }

    > div:nth-of-type(1) {
      text-align: left;
      display: flex;
      align-items: center;
    }

    > div:nth-of-type(3) {
      text-align: right;
    }
  }

  > div:nth-of-type(3) {
    display: flex;
    flex-direction: column;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;

    > div:nth-of-type(1) {
      display: flex;
      flex-direction: row;
      align-items: center;

      > div:nth-of-type(1) {
        display: flex;
        flex-direction: row;
        align-items: center;
      }

      .div-handle-right {
        flex: 1;
        flex-direction: row-reverse;
        text-align: right;
        display: flex;
        align-items: center;

        > input:nth-of-type(1) {
          border: 1px solid #eeeeee;
          font-size: 15px;
          width: 150px;
          padding: 5px 8px;
          outline: none;
        }

        > button {
          color: #a866ff;
          margin-left: 10px;
          padding: 4px 10px;
        }
      }

      > div:nth-of-type(2),
      > div:nth-of-type(3) {
        margin-left: 10px;
      }
    }
  }

  > div:nth-of-type(4) {
    flex-direction: column;
  }
}
</style>
